// page/matchEnrollWrite/matchEnrollWrite.js
let http = require('../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '赛事报名',
    maskFlag: true,
    popupViewPay: true,
    popupViewEnroll: true,
    sexIndex: 0,
    groupIndex: 0,
    sexNum: 0,
    groupNum: 0,
    sexArray: ['男', '女'],
    groupArray: ['组1', '组2', '组3']
  },
  // 性别选择
  sexChange: function (e) {
    this.setData({
      sexIndex: e.detail.value,
      sexUp:Number(e.detail.value) + 1,
      sexNum: 1
    })
  },
  // 监听name
  nameEnroll(e) {
    let nameEnroll = e.detail.value
    if (nameEnroll == '') {
      wx.showToast({
        title: '姓名不能为空',
        icon: 'none'
      })
    } else {
      this.setData({
        nameEnroll
      })
    }
  },
  // 监听身份证
  idcard(e) {
    let idcard = e.detail.value
    if (idcard == '') {
      wx.showToast({
        title: '身份证不能为空',
        icon: 'none'
      })
    } else {
      this.setData({
        idcard
      })
    }
  },
  // 父亲电话
  fatherPhone(e) {
    let fatherPhone = e.detail.value
    if (fatherPhone == '') {
      wx.showToast({
        title: '父亲电话不能为空',
        icon: 'none'
      })
    } else {
      this.setData({
        fatherPhone
      })
    }
  },
  // 母亲电话
  motherPhone(e) {
    let motherPhone = e.detail.value
    if (motherPhone == '') {
      wx.showToast({
        title: '母亲电话不能为空',
        icon: 'none'
      })
    } else {
      this.setData({
        motherPhone
      })
    }
  },
  // 显示遮罩及打开自定义支付弹框
  showPayPopup(e) {
    let _this = this
    let nameEnroll = _this.data.nameEnroll
    let fatherPhone = _this.data.fatherPhone
    let motherPhone = _this.data.motherPhone
    let idcard = _this.data.idcard
    let matchid = _this.data.matchid
    let sexUp = _this.data.sexUp
    let sexIndex=_this.data.sexIndex

    if (nameEnroll&&fatherPhone&&fatherPhone&&idcard&&matchid&&sexIndex!=='') {
      http.postHttp('/create_order', {
        competition_id: matchid,
        card_number: idcard,
        name: nameEnroll,
        sex: sexUp,
        f_phone: fatherPhone,
        m_phone: motherPhone,
        scene: 3
      }, {
        token: wx.getStorageSync('token')
      }).then(res => {
        console.log('赛事报名创建', res)
        this.setData({
          appId: res.data.data.appId,
          nonceStr: res.data.data.nonceStr,
          packageInfo: res.data.data.package,
          paySign: res.data.data.paySign,
          signType: res.data.data.signType,
          timeStamp: res.data.data.timeStamp
        })
      })
      this.setData({
        maskFlag: false,
        popupViewPay: false,
      })

    }else{
      wx.showToast({
        title: '输入框不能为空',
        icon:'none'
      })
    }
  },
  //弹出支付请求
  showPaySuccess(e) {
    let _this = this
    wx.requestPayment({
      timeStamp: _this.data.timeStamp,
      nonceStr: _this.data.nonceStr,
      package: _this.data.packageInfo,
      signType: _this.data.signType,
      paySign: _this.data.paySign,
      success(res) {
        console.log(res, '支付成功')
        _this.setData({
          maskFlag: false,
          popupViewPay: true,
          popupViewEnroll: false
        })
      },
      fail(res) {
        wx.showToast({
          title: '支付出错',
          icon: 'none'
        })
        console.log(res, '支付失败')
      }
    })

  },
  // 返回主页
  goBackIndex(e) {
    wx.redirectTo({
      url: '/pages/myIndex/myIndex',
    })
  },
  // 关闭popup
  closePopop(e) {
    this.setData({
      maskFlag: true,
      popupViewPay: true,
      popupViewEnroll: true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let matchid = options.matchid
    let matchPrice = options.matchPrice
    this.setData({
      matchid,
      matchPrice
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})